//
//  SubscribeController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/22.
//

#import "SubscribeController.h"
#import "HandleNetProduct+Transaction.h"
#import "RansomAlertController.h"
#import "ConsultantController.h"
#import "NewRansomController.h"
#import "CKPuslichTool.h"
#import "UIColor+NN.h"
#import "RansomMangerCell.h"
#import "RansomOrderCell.h"

@interface SubscribeController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *subscribeTable;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (nonatomic, strong) UIView *myHeaderView;

@property (nonatomic, assign) BOOL isMore;
@property (nonatomic, strong) NSDictionary *detailDic;
@property (nonatomic, strong) NSMutableArray *list;

@end

@implementation SubscribeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详细信息";
    _subscribeTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _myHeaderView = _headerView;
    
    [self getRenGouInfoAction];
}

- (NSMutableArray *)list {
    if (!_list) {
        _list = [NSMutableArray array];
    }
    return _list;
}

//订单信息
- (void)getRenGouInfoAction {
    [HandleNetProduct renGouDetailInfoRansomID:_redeemId Success:^(NSDictionary * _Nonnull data) {
        _detailDic = data;
        [self getOrderPayListAction: data[@"ContractNo"]];
        [_subscribeTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

//交易记录
- (void)getOrderPayListAction:(NSString *)ContractNo {
    [HandleNetProduct getOrderPayListWithBookingId:_redeemId ContractNo:ContractNo Success:^(NSArray * _Nonnull data) {
        self.list = [data mutableCopy];
        [_subscribeTable reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 && _detailDic) {
        if (_isMore) {
            return 13;
        }
        else {
           return 4;
        }
    }
    else {
        return _list.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }
    else {
        return 110;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1 && _list.count > 0) {
        return 50;
    }
    else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return _myHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0 && !_isMore && _detailDic) {
        return 50;
    }
    else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        view.backgroundColor = [UIColor whiteColor];
        UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        [moreBtn setTitle:@"查看更多" forState:UIControlStateNormal];
        [moreBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        moreBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [moreBtn addTarget:self action:@selector(clickMordAction) forControlEvents:UIControlEventTouchUpInside];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, self.view.frame.size.width-15, 0.5)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#cccccc"];
        [view addSubview:lineView];
        [view addSubview:moreBtn];
        return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        RansomOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RansomOrderCell3"];
        if (_detailDic) {
            [cell setRenGouDetailAction:_detailDic index:indexPath.row];
        }
        return cell;
    }
    else {
        RansomMangerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RansomMangerCell2"];
        cell.separatorInset = UIEdgeInsetsMake(0, self.view.frame.size.width, 0, 0);
        [cell setRenGouListData:_list[indexPath.row]];
        return cell;
    }
}

- (IBAction)clickRansomAction:(UIButton *)sender {
    if ([_detailDic[@"HasMessage"] integerValue] == 1) {
        NewRansomController *vc = (NewRansomController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"NewRansomController"];
        vc.contractNo = _detailDic[@"ContractNo"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        RansomAlertController *vc = (RansomAlertController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomAlertController"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
        vc.yuyueText = _detailDic[@"WarmingMessage"];
        vc.returnBlock = ^(NSInteger type) {
            switch (type) {
                case 1:
                {
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 2:
                {
                    ConsultantController *vc = (ConsultantController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"ConsultantController"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
            }
        };
        [self presentViewController:vc animated:YES completion:nil];
    }
}

- (void)clickMordAction {
    _isMore = YES;
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
    [_subscribeTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end
